package org.example.vue3tsbackend.web;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import org.example.vue3tsbackend.common.RestResult;
import org.example.vue3tsbackend.po.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @GetMapping("/current")
    public RestResult<User> getCurrentUser() {
        User user = new User();
        user.setName("张三");
        user.setId("111");
        user.setUserRole(1);
        return RestResult.ok(user);
    }

    @PostMapping("/login")
    public RestResult login(@RequestBody User user) {
        log.info(user.getAccount() + "-----------" + user.getPassword());
        return RestResult.ok(user);
    }

    @PostMapping("/register")
    public RestResult register(@RequestBody User user) {
        log.info(JSON.toJSONString(user));
        return RestResult.ok(user);
    }

    @GetMapping("/search")
    public RestResult<List<User>> search(@RequestParam(required = false) String username) {
        log.info("user:{}", username);
        User user1 = new User();
        user1.setName("张三");
        user1.setId("111");
        user1.setUserRole(1);
        user1.setAccount("zs");
        user1.setTx("https://picx.zhimg.com/v2-e38215dc16e1d9987460e47e87c94210_720w.jpg?source=172ae18b");
        user1.setXb("1");
        user1.setCreateTime(DateUtil.date());
        User user2 = new User();
        user2.setName("李四");
        user2.setId("222");
        user2.setUserRole(2);
        user2.setAccount("ls");
        user2.setTx("https://picx.zhimg.com/v2-e38215dc16e1d9987460e47e87c94210_720w.jpg?source=172ae18b");
        user2.setXb("2");
        user2.setCreateTime(DateUtil.date());
        List<User> users = Lists.newArrayList(user1, user2);
        if (StrUtil.isNotEmpty(username)) {
            users = users.stream().filter(x -> StrUtil.equals(x.getName(), username)).toList();
        }
        return RestResult.ok(users);
    }

    @PostMapping("/delete")
    public RestResult<String> delete(@RequestBody String id) {
        log.info(JSON.toJSONString(id));
        return RestResult.ok(id);
    }

}
